package jp.yvn.game.shooting.view;

import jp.yvn.game.shooting.R;
import jp.yvn.game.shooting.element.BackGroundEntity;
import jp.yvn.game.shooting.element.FpsEntity;
import jp.yvn.game.shooting.element.MainCharactorEntity;
import jp.yvn.game.shooting.element.TouchAreaEntity;
import jp.yvn.game.shooting.logic.StageLogic;
import jp.yvn.game.shooting.schedule.NomalStage1Stage;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * レンダリングをします
 * 
 * @author Yvn_pq
 */
public class Renderer extends SurfaceView implements Runnable, SurfaceHolder.Callback, OnGestureListener, OnTouchListener {

	/** スレッド */
	private Thread mThread;

	/** 主人公 */
	private MainCharactorEntity mMain;

	/** 背景 */
	private BackGroundEntity mBackGroundElement;

	/** タッチエリア */
	private TouchAreaEntity mTouchArea;

	/** GestureDetector */
	private final GestureDetector mGesture;

	/** 一時停止フラグ */
	public static boolean isPause;

	/** 敵 */
	private StageLogic mStage;

	/**
	 * コンストラクタ
	 * 
	 * @param context
	 * @param attrs
	 */
	public Renderer(Context context, AttributeSet attrs) {
		super(context, attrs);
		getHolder().addCallback(this);
		setClickable(true);
		setOnTouchListener(this);
		mGesture = new GestureDetector(context, Renderer.this);
		isPause = false;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// 初期化
		mMain = new MainCharactorEntity(getContext(), width, height); // 自機
		mBackGroundElement = new BackGroundEntity(getContext(), BackGroundEntity.NOMAL_STG_1_BMP_ID, width, height); // 背景
		mTouchArea = new TouchAreaEntity(getContext(), width, height); // コマンド
		NomalStage1Stage stage1 = new NomalStage1Stage(getContext(), new Point(width, height));
		mStage = new StageLogic(getContext(), stage1);

		// レンダリング開始
		mThread.start();
	}

	/*
	 * 開始処理をします
	 * 
	 * @see
	 * android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder
	 * )
	 */
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mThread = new Thread(this);
	}

	/*
	 * 終了処理をします
	 * 
	 * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.
	 * SurfaceHolder)
	 */
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.v(getContext().getString(R.string.app_name), "Renderer SurfaceDestroyed mThread:" + mThread);
		if (mThread != null) {
			mThread = null;
		}
	}

	/*
	 * レンダリング部分です
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		Canvas canvas = new Canvas();
		FpsEntity fps = new FpsEntity();

		while (mThread != null) {
			canvas = getHolder().lockCanvas();

			// 一時停止
			if (isPause) {
				mBackGroundElement.drawPause(canvas);// 一時停止
			}
			// 一時停止以外
			else {
				/* 描画部分 */
				mBackGroundElement.draw(canvas);// 背景描画
				mTouchArea.draw(canvas); // タッチエリア描画
				mMain.action(); // 主人公状態更新
				// mStage.update(); // ステージイベント更新
				mStage.draw(canvas); // ステージイベント描画
				mMain.draw(canvas); // メインキャラ描画
			}
			fps.draw(canvas); // FPS更新
			getHolder().unlockCanvasAndPost(canvas);

			// FPSの更新
			fps.updateFps();
		}
	}

	/*
	 * タッチ処理を行います
	 * 
	 * @see android.view.View#onTouchEvent(android.view.MotionEvent)
	 */
	@Override
	public boolean onTouch(View view, MotionEvent motionevent) {
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
		}
		if (motionevent.getAction() == MotionEvent.ACTION_MOVE) {
			mTouchArea.touched(motionevent.getX(), motionevent.getY());
			mMain.setMove(mTouchArea);
			return true;
		}
		if (motionevent.getAction() == MotionEvent.ACTION_UP) {
			mTouchArea.reset();
			mMain.setMove(mTouchArea);
			return true;
		}
		return mGesture.onTouchEvent(motionevent);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		if (mTouchArea.touched(e.getX(), e.getY())) {
			mMain.setMove(mTouchArea);
			return true;
		}
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// 一時停止タッチアップで解除
		if (isPause) {
			isPause = false;
		}
		return false;
	}
}
